﻿Imports System.Globalization

Public Module Utils

    <System.Runtime.CompilerServices.Extension()> _
    Public Function Where(Of TSource)(source As IEnumerable(Of TSource), isThucThi As Boolean, predicate As Func(Of TSource, Boolean)) As IEnumerable(Of TSource)
        If isThucThi Then
            Return source.Where(predicate)
        End If

        Return source
    End Function

    Private ReadOnly VietnameseSigns As String() = New String() {"aAeEoOuUiIdDyY", "áàạảãâấầậẩẫăắằặẳẵ", "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ", "éèẹẻẽêếềệểễ", "ÉÈẸẺẼÊẾỀỆỂỄ", "óòọỏõôốồộổỗơớờợởỡ", "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ", "úùụủũưứừựửữ", "ÚÙỤỦŨƯỨỪỰỬỮ", "íìịỉĩ", "ÍÌỊỈĨ", "đ", "Đ", "ýỳỵỷỹ", "ÝỲỴỶỸ"}
    ''' <summary>
    ''' Lọc tiếng việt có dấu thành không dấu
    ''' </summary>
    ''' <param name="str"></param>
    ''' <returns></returns>
    <System.Runtime.CompilerServices.Extension()> _
    Public Function BoDau(str As String) As String

        'Tiến hành thay thế , lọc bỏ dấu cho chuỗi

        Dim i As Integer = 1
        While i < VietnameseSigns.Length

            Dim j As Integer = 0
            While j < VietnameseSigns(i).Length

                str = str.Replace(VietnameseSigns(i)(j), VietnameseSigns(0)(i - 1))
                j += 1


            End While
            i += 1
        End While

        Return str

    End Function

    <System.Runtime.CompilerServices.Extension()> _
    Public Function Format(ByVal dec As Integer, Optional ByVal strDauHangTram As String = ".", _
                          Optional ByVal intDauThapPhan As Integer = 0, Optional ByVal strHauTo As String = "") As String
        Return Convert.ToDecimal(dec).Format(strDauHangTram, intDauThapPhan, strHauTo)
    End Function

    <System.Runtime.CompilerServices.Extension()> _
    Public Function Format(ByVal dec As Decimal, Optional ByVal strDauHangTram As String = ".", _
                          Optional ByVal intDauThapPhan As Integer = 0, Optional ByVal strHauTo As String = "") As String
        If ((dec <= 999) AndAlso (intDauThapPhan = 0)) Then
            Return dec.ToString
        End If
        Dim strFormat As String = "#,##0"
        Dim i As Integer
        For i = 0 To intDauThapPhan - 1
            If strFormat.EndsWith("#,##0") Then
                strFormat = (strFormat & ".")
            End If
            strFormat = (strFormat & "0")
        Next i
        If Not String.IsNullOrEmpty(strHauTo) Then
            strFormat = (strFormat & " " & strHauTo)
        End If
        If strDauHangTram.Equals(",") Then
            Return dec.ToString(strFormat, CultureInfo.CreateSpecificCulture("en-us"))
        End If
        Return dec.ToString(strFormat, CultureInfo.CreateSpecificCulture("nl-NL"))
    End Function

End Module
